package com.jinlia.show.modules.wxapp.controller;

import com.jinlia.show.common.core.utils.UserInfoUtils;
import com.jinlia.show.modules.base.model.form.MemberCardForm;
import com.jinlia.show.modules.base.system.entity.MemberCard;
import com.jinlia.show.modules.base.system.entity.MemberCardBalanceChange;
import com.jinlia.show.modules.base.system.entity.MemberCardScoreChange;
import com.jinlia.show.modules.base.system.service.MemberCardBalanceChangeMapperService;
import com.jinlia.show.modules.base.system.service.MemberCardMapperService;
import com.jinlia.show.modules.base.system.service.MemberCardScoreChangeMapperService;
import com.jinlia.show.modules.wxapp.service.MemberCardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/member/card")
public class MemberCardController {

    @Autowired
    private MemberCardService memberCardService;
    @Autowired
    private MemberCardMapperService memberCardMapperService;
    @Autowired
    private MemberCardBalanceChangeMapperService memberCardBalanceChangeMapperService;
    @Autowired
    private MemberCardScoreChangeMapperService memberCardScoreChangeMapperService;

    /**
     * 绑定会员卡
     *
     * @param memberCard
     * @return
     */
    @PostMapping("bind")
    public ResponseEntity<MemberCard> bind(@RequestBody MemberCardForm memberCard) {
        return ResponseEntity.ok(memberCardService.addMemberCard(memberCard));
    }

    /**
     * 获取会员卡
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<MemberCard> getById(@PathVariable(value = "id") Long id) {
        return ResponseEntity.ok(memberCardMapperService.getById(id));
    }

    /**
     * 余额变化记录
     *
     * @return
     */
    @PostMapping("balance/change")
    public ResponseEntity<List<MemberCardBalanceChange>> getBalanceChange() {
        List<MemberCardBalanceChange> changes = memberCardBalanceChangeMapperService.getBalanceChange(UserInfoUtils.getUserId());
        return ResponseEntity.ok(changes);
    }

    /**
     * 积分变化记录
     *
     * @return
     */
    @PostMapping("score/change")
    public ResponseEntity<List<MemberCardScoreChange>> getScoreChange() {
        List<MemberCardScoreChange> changes = memberCardScoreChangeMapperService.getScoreChange(UserInfoUtils.getUserId());
        return ResponseEntity.ok(changes);
    }
}
